home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / leefilt.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  135 lines

  1. ; $Id: leefilt.pro,v 1.3 1996/12/17 23:45:06 lubos Exp $
  2.  
  3. Function Lee_filter_exact,A,N,SIG
  4. ;
  5. ; Slow, but accurate Lee filter. Derived from
  6. ; Jong-Sen Lee, Optical Engineering 25(5), 636-643 (May 1986)
  7. ;
  8.   Z=(AA=FLOAT(A))
  9.   Delta=(T_Y=(T_X=(N-1)/2))
  10.   SZ=size(AA) & N_SAMPLES=SZ[1]
  11. ;
  12.   mean=smooth(AA,N,/EDGE_TRUNCATE) & mean2=mean^2.
  13. ;
  14.   IF SZ[0] eq 1 THEN BEGIN
  15.      FOR S=T_X,N_SAMPLES-T_X-1 DO $
  16.         Z[S]=TOTAL((AA[S-Delta:S+Delta] - mean[S])^2.)
  17.   ENDIF ELSE BEGIN
  18.      N_LINES=SZ[2]
  19. ;
  20. ; Compute Variance of Z
  21. ;
  22.     FOR L=T_Y, N_LINES-T_Y-1 DO $
  23.       FOR S=T_X,N_SAMPLES-T_X-1 DO $
  24.         Z[S,L]=TOTAL((AA[S-Delta:S+Delta,L-Delta:L+Delta] - mean[S,L])^2.)
  25.   ENDELSE
  26. ;
  27. ; Upon starting the next equation,  Z = Var(Z). Upon exit, Z = Var(X) 
  28. ; of equation 19 of Lee, Optical Engineering 25(5), 636-643 (May 1986)
  29. ;
  30. ; VAR_X = (VAR_Z + Mean^2 )/(Sigma^2 +1) - Mean^2   (19)
  31. ;
  32.   Z=( (TEMPORARY(Z) +mean2) /(1+Sig^2.)) - mean2 
  33. ;
  34. ; return value from equation 21,22 of Lee, 1986.
  35. ; K = ( VAR_X/(mean^2 * Sigma^2 + VAR_X) )          (22)
  36. ; Filtered_Image = Mean + K * ( Input_Image - Mean) (21)
  37.   return, mean + (AA-mean) * ( Z/(mean2*Sig^2. + Z) ) 
  38. END
  39.  
  40. Function Lee_filter_fast,A,N,SIG
  41. ; Faster, but less accurate Lee filter
  42. ; Only recomended when N < 7
  43. ;
  44.   AA=FLOAT(A)
  45. ;
  46.   mean=SMOOTH(AA,N,/EDGE_TRUNCATE) & mean2=mean^2. 
  47.   AA=AA-mean
  48. ;
  49.  VAR_X=((SMOOTH(AA^2.,N,/EDGE_TRUNCATE)+mean2)/(1.+Sig^2.)) - mean2
  50. ;
  51. ; return value from equation 21,22 of Lee, 1986.
  52. ; K = ( VAR_X/(mean^2 * Sigma^2 + VAR_X) )          (22)
  53. ; Filtered_Image = Mean + K * ( Input_Image - Mean) (21)
  54.  RETURN, mean + AA * (VAR_X / ( mean2 * Sig^2. + VAR_X))
  55. ;
  56. END
  57.  
  58. Function Leefilt, A, N, Sig,EXACT=EXACT
  59. ;+
  60. ; NAME:
  61. ;    LEEFILT
  62. ;
  63. ; PURPOSE:
  64. ;    Performs the Lee filter algorithm on an image array using a 
  65. ;    box of size 2N+1.  This function can also be used on vectors.
  66. ;
  67. ; CATEGORY:
  68. ;    E3 Smoothing (image).
  69. ;
  70. ; CALLING SEQUENCE:
  71. ;    Result = LEEFILT(A [, N, Sig])
  72. ;
  73. ; INPUTS:
  74. ;    A:    The input image array or one-dimensional vector.
  75. ;
  76. ; OPTIONAL INPUT PARAMETERS:
  77. ;    N:    The size of the filter box is 2N+1.  The default value is 5.
  78. ;
  79. ;    Sig:    Estimate of the standard deviation.  The default is 5.
  80. ;        If Sig is negative the procedure requests a value to be 
  81. ;        entered, and displays the resulting image or vector.  This 
  82. ;               cycle continues until a zero value of Sig is entered.
  83. ;
  84. ; KEYWORDS:
  85. ;    EXACT:  Use this keyword to use a more accurate, but much slower
  86. ;        Lee filter algorithm. Recommended when N > ~7.
  87. ; OUTPUTS:
  88. ;    The filtered image or vector is returned.
  89. ;
  90. ; COMMON BLOCKS:
  91. ;    None.
  92. ;
  93. ; SIDE EFFECTS:
  94. ;    Displays the filtered image in an IDL window using TVSCL or PLOT if 
  95. ;       Sig is negative.
  96. ;
  97. ; RESTRICTIONS:
  98. ;    None.
  99. ;
  100. ; PROCEDURE:
  101. ;    The LEE (Optical Engineering, Vol 25, No 5, Pg 636-643, May 1986) 
  102. ;       technique smooths additive image noise by generating statistics in
  103. ;       a local neighborhood and comparing them to the expected values.
  104. ;
  105. ; MODIFICATION HISTORY:
  106. ;    Written, 24-Nov-1982, by R. A. Howard, Naval Research Lab,
  107. ;                 Washington, DC 20375
  108. ;       Modified, 30-May-1996, SVP. Modifed to match 1986 algorithm &
  109. ;                              Added /EXACT at user suggestion.
  110. ;                              Added PLOT for vector support.
  111. ;-
  112. ;
  113. ON_ERROR,2                      ;Return to caller if an error occurs
  114. NP = N_params(0)
  115. IF np lt 3 THEN Sig = 5. else Sig=FLOAT(Sig)    ;supply defaults
  116. IF np lt 2 THEN n = 5
  117. pl = sig LE 0.            ;true if interactive mode
  118. loop:
  119.     IF pl THEN read,'Type in Sigma (0 to quit) :  ',sig
  120.     IF sig EQ 0 THEN goto,endp
  121.         IF keyword_set(EXACT) THEN $
  122.            f=lee_filter_exact(a,2*n+1,Sig)  ELSE $
  123.        f=lee_filter_fast(a,2*n+1,Sig) 
  124.     IF pl THEN BEGIN
  125.            IF (size(f))[0] eq 1 then $
  126.            PLOT,f,xtitle='Element of A',ytitle='Value of A',$
  127.                 title='Lee Filtered A, N ='+string(N,FORMAT='(I3)')+', Sigma = '+$
  128.                 string(Sig,FORMAT='(F5.2)') else TVSCL,f
  129.        GOTO , loop
  130.     ENDIF
  131. endp:    RETURN,f
  132. END
  133.